class_name WeaponComponent
extends BaseComponent

var default_weapon_name:String
var weapon_max_amount:int = 1
var cur_weapon_index:int = -1
var cur_weapon:Weapon = null:
	get:
		return cur_weapon
	set(value):
		var last_weapon = cur_weapon
		cur_weapon = value
		cur_weapon_changed.emit(last_weapon,cur_weapon)
var weapons:Array[Weapon] = []
var slot_component:SlotComponent
var controller_component:ControllerComponent
signal cur_weapon_changed(last_weapon:Weapon,cur_weapon:Weapon)
signal weapons_changed(weapons:Array[Weapon])

func get_cur_weapon() -> Weapon:
	return cur_weapon
func get_weapon(idx:int) -> Weapon:
	if idx < weapons.size():
		return weapons[idx]
	return null
func obtain_weapon(weapon:Weapon):
	if weapon and weapon_max_amount > 0:
		if cur_weapon:
			if weapons.size() == weapon_max_amount:
				cur_weapon.discard()
			else:
				cur_weapon.unequip()
		var hand = slot_component.get_slot_by_type(GameEnum.SlotType.Hand)
		weapon.equip(cur_entity)
		if weapon.get_parent():
			weapon.get_parent().remove_child(weapon)
		hand.add_child(weapon)
		weapons.append(weapon)
		cur_weapon_index = weapons.size() - 1
		cur_weapon = weapon
		weapons_changed.emit(weapons)
func obtain_weapon_by_name(weapon_name:String):
	var weapon:Weapon = GameWeaponSystem.get_weapon(weapon_name) as Weapon
	if weapon and weapon_max_amount > 0:
		if cur_weapon:
			if weapons.size() == weapon_max_amount:
				cur_weapon.discard()
			else:
				cur_weapon.unequip()
		var hand = slot_component.get_slot_by_type(GameEnum.SlotType.Hand)
		weapon.equip(cur_entity)
		hand.add_child(weapon)
		weapons.append(weapon)
		cur_weapon_index = weapons.size() - 1
		cur_weapon = weapon
		weapons_changed.emit(weapons)
func discard_weapon(idx = null):
	if not idx:
		idx = cur_weapon_index
	if idx < weapons.size():
		var weapon:Weapon = weapons[idx]
		#if weapons.size() == 1 and cur_weapon.weapon_data.weapon_name == default_weapon_name and weapon_max_amount > 0:
			#return
		weapon.discard()
		if idx == cur_weapon_index:
			if weapons.size() == 1 or (cur_weapon_index + 1) % weapons.size() >= weapon_max_amount:
				cur_weapon_index = -1
				cur_weapon = null
			else:
				cur_weapon_index = (cur_weapon_index + 1) % weapons.size() 
				cur_weapon = weapons[cur_weapon_index]
				cur_weapon.equip(cur_entity)
		weapons.erase(weapon)
		weapons_changed.emit(weapons)
func switch_weapon(index:int = -1):
	if index == cur_weapon_index or weapon_max_amount <= 1:
		return
	index = (cur_weapon_index + 1) % weapons.size() if index == -1 else index
	if cur_weapon:
		cur_weapon.unequip()
	cur_weapon_index = index
	cur_weapon = weapons[cur_weapon_index]
	cur_weapon.equip(cur_entity)
func _on_cur_weapon_changed(last_weapon:Weapon,cur_weapon:Weapon):
	pass
func _on_weapons_changed(weapons:Array):
	if weapons.size() == 0:
		obtain_weapon_by_name(default_weapon_name)
func _on_key_action(key_action:GameEnum.KeyAction,key_action_state:GameEnum.KeyActionState):
	if key_action == GameEnum.KeyAction.Discard and key_action_state == GameEnum.KeyActionState.Pressed:
		discard_weapon()
	if key_action == GameEnum.KeyAction.Interact and key_action_state == GameEnum.KeyActionState.Pressed:
		if cur_entity.interact_items.size() > 0:
			cur_entity.interact_items[0].start_interact(cur_entity)
	if key_action == GameEnum.KeyAction.Interact and key_action_state == GameEnum.KeyActionState.Release:
		if cur_entity.interact_items.size() > 0:
			cur_entity.interact_items[0].end_interact(cur_entity)
func weapon_max_amount_changed(change_tag:String):
	if change_tag == "weapon_max_amount":
		weapon_max_amount = cur_entity.get_entity_data().weapon_max_amount
		for i in range(weapons.size()-1,-1,-1):
			if i >= weapon_max_amount:
				discard_weapon(i)
	elif change_tag == "default_weapon_name":
		default_weapon_name = cur_entity.get_entity_data().default_weapon_name

func init_component(entity):
	super.init_component(entity)
	slot_component = cur_entity.get_component_by_name("SlotComponent")
	controller_component = cur_entity.get_component_by_name("ControllerComponent")
	cur_entity.get_entity_data().data_changed.connect(weapon_max_amount_changed)
	cur_weapon_changed.connect(_on_cur_weapon_changed)
	weapons_changed.connect(_on_weapons_changed)
	weapon_max_amount = cur_entity.get_entity_data().weapon_max_amount
	default_weapon_name = cur_entity.get_entity_data().default_weapon_name
	controller_component.on_key_action.connect(_on_key_action)
	if weapons.size() == 0:
		obtain_weapon_by_name(default_weapon_name)
	
func _init() -> void:
	component_name = "WeaponComponent"
	priority = 10 if priority == 0 else priority
